package cn.springboot.erp.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.springboot.erp.annotation.Log;
import cn.springboot.erp.commons.QueryParams;
import cn.springboot.erp.commons.ResponseResult;
import cn.springboot.erp.entity.Suppliers;
import cn.springboot.erp.enums.Operation;
import cn.springboot.erp.service.SuppliersService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("suppliers")
@Api(tags = "供应商管理模块")
public class SuppliersController {

    @Resource
    private SuppliersService suppliersService;

    @RequiresPermissions("base:suppliers:select")
    @Log(description = Operation.SELECT,model = "供应商管理")
    @ApiOperation("数据分页")
    @PostMapping("page")
    public ResponseResult listPage(@RequestBody QueryParams<Suppliers> queryParams){
        return suppliersService.listPage(queryParams);
    }

    @RequiresPermissions("base:suppliers:select")
    @Log(description = Operation.SELECT,model = "供应商管理")
    @ApiOperation("根据id查询")
    @GetMapping("selectById/{id}")
    public ResponseResult selectById(@PathVariable("id") Integer id){
        Suppliers supplier = suppliersService.getById(id);
        return ResponseResult.Success().data("supplier",supplier);
    }

    @RequiresPermissions("base:suppliers:delete")
    @Log(description = Operation.DELETE,model = "供应商管理")
    @ApiOperation("根据id删除")
    @GetMapping("deleteById/{id}")
    public ResponseResult deleteById(@PathVariable("id") Integer id){
        suppliersService.removeById(id);
        return ResponseResult.Success().msg("删除成功");
    }

    @RequiresPermissions(value = {"base:suppliers:add","base:suppliers:update"})
    @Log(description = Operation.ADDORUPDATE,model = "供应商管理")
    @ApiOperation("添加或编辑")
    @PostMapping("insertOrUpdate")
    public ResponseResult insertOrUpdate(@RequestBody Suppliers suppliers) {
        // 获取供应商名称
        String name = suppliers.getName();
        // 根据供应商名称查询数据库
        LambdaQueryWrapper<Suppliers> lambda = new QueryWrapper<Suppliers>().lambda();
        lambda.eq(Suppliers::getName, name);
        Suppliers one = suppliersService.getOne(lambda);

        // 如果供应商信息存在
        if (ObjectUtils.isNotNull(one)) {
            // 比较id，id相同则表示是同一个供应商信息，做编辑操作
            if (ObjectUtil.equal(one.getId(), suppliers.getId())) {
                suppliersService.saveOrUpdate(suppliers);
                return ResponseResult.Success().msg("编辑成功");
            }
            return new ResponseResult().Fail().msg("该供应商已经存在");
        }
        // 供应商信息不存在，做添加操作
        suppliersService.saveOrUpdate(suppliers);
        return ResponseResult.Success().msg("添加成功");
    }


    @RequiresPermissions("base:suppliers:add")
    @Log(description = Operation.ADD,model = "供应商管理")
    @ApiOperation("批量导入")
    @PostMapping("import")
    public ResponseResult importSuppliers(MultipartFile file) throws Exception {
        return suppliersService.importCategories(file);
    }

    @RequiresPermissions("base:suppliers:select")
    @Log(description = Operation.ADD,model = "供应商管理")
    @ApiOperation("批量导出")
    @PostMapping("export")
    public void exportSuppliers(@RequestBody Map<String,Object> params, HttpServletResponse response) throws IOException {
        suppliersService.exportSuppliers(params,response);
    }
}
